<template>
	<!-- 音频组件 -->
	<view>
		<!-- 音频组件1 -->
		<view class="wC_audio1" v-if="audio == 'wAudio1'">
			<view class="wC_audio1-box">
				<!-- 封面图 -->
				<view class="wC_audio1-poster" v-if="poster">
					<image class="wC_audio1-posterImg" :src="poster" mode="aspectFill"></image>
				</view>
				<!-- 播放图标 -->
				<view class="wC_audio1-play">
					<view class="wC_audio1-btn" @tap="wCFun_handleClick">
						<!-- 三角点击播放按钮样式 -->
						<view class="wCA1BI-play" v-if="wState == 'play'">
							<view class="wCA1BIPlay-icon"></view>
						</view>
						<!-- 三角点击播放按钮样式 -->
						<view class="wCA1BI-play" v-if="wState == 'stop'">
							<view class="wCA1BIPlay-icon"></view>
						</view>
						<!-- 正方形点击暂停按钮样式 -->
						<view class="wCA1BI-pause" v-if="wState == 'pause'">
							<view class="wCA1BIPause-icon"></view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "wc_audios",
		props: {
			// 音频组件类型
			audio: {
				type: String,
				default: 'wAudio1',
			},
			// 当前节点数据对象
			obj: {
				type: Object,
				default: {},
			},
			// 音频地址
			src: {
				type: String,
				default: '',
			},
			// 封面图
			poster: {
				type: String,
				default: require('@/static/images/logo.png'), // 封面图
			},
		},
		data() {
			return {
				wInnerAudioContext: null, // 音乐播放组件 innerAudioContext 对象
				wState: 'stop', // play-可播放(继续播放)，pause-可暂停，stop-重置(重头播)
			};
		},
		created() {
			this.wCFun_audioInit();

			// 关闭音乐播放-实现效果关闭所有音乐
			uni.$on('wAudioCloseAll', () => {
				this.wState = "stop";
				this.wCFun_handleAudio('stop');
			})
		},
		beforeDestroy() {
			this.wState = "stop";
			this.wCFun_handleAudio('stop');
		},
		methods: {
			// 组件方法-点击事件
			wCFun_handleClick() {
				console.log(this.wState)
				// this.$emit('wPlayOrPause', this.obj);
				if (this.wState == 'play') {
					this.wState = "pause";
					this.wCFun_handleAudio('play', this.src);
					return;
				}
				if (this.wState == 'pause') {
					this.wState = 'play'
					this.wCFun_handleAudio('pause');
					return;
				}
				if (this.wState == 'stop') {
					this.wState = "pause";
					this.wCFun_handleAudio('play', this.src);
					return;
				}
			},

			// 组件方法-音频播放初始化
			wCFun_audioInit() {
				// 判断是否存在全局播放器
				if (!this.wInnerAudioContext) {
					this.wInnerAudioContext = uni.createInnerAudioContext();
				}
				// 音频播放事件
				this.wInnerAudioContext.onPlay(() => {
					// this.wState = 'pause';
				});
				// 音频暂停事件
				this.wInnerAudioContext.onPause(() => {
					// this.wState = 'play';
				});
				// 音频停止事件
				this.wInnerAudioContext.onStop(() => {
					// this.wState = 'stop';
				});
				// 音频自然播放结束事件
				this.wInnerAudioContext.onEnded(() => {
					this.wState = 'stop';
				});
				// 音频播放错误事件
				this.wInnerAudioContext.onError((err) => {
					this.wState = 'stop';
					console.log(err);
					if (err.errCode == 10001) {
						uni.showToast({
							title: "系统错误,音频播放失败！",
							icon: 'error',
							mask: true,
						})
					} else if (err.errCode == 10002) {
						uni.showToast({
							title: "网络错误,音频播放失败！",
							icon: 'error',
							mask: true,
						})
					} else if (err.errCode == 10003) {
						uni.showToast({
							title: "文件错误,音频播放失败！",
							icon: 'error',
							mask: true,
						})
					} else if (err.errCode == 10004) {
						uni.showToast({
							title: "格式错误,音频播放失败！",
							icon: 'error',
							mask: true,
						})
					} else {
						console.log("未知错误！");
					}
				});
			},

			// 组件方法-音乐播放操作
			wCFun_handleAudio(wP_state, wP_src) {
				// console.log(wP_state, wP_src)
				// 默认重置所有的音乐播放
				if (wP_state != 'pause' && wP_state != 'play') {
					wP_state = 'stop'
				}
				// 判断是否存在全局播放器
				if (!this.wInnerAudioContext) {
					this.wInnerAudioContext = uni.createInnerAudioContext();
				}
				this.$nextTick(() => {
					// 暂停正在播放的音乐
					if (wP_state == 'pause') {
						this.wInnerAudioContext.pause();
					}
					// 重置正在播放的音乐
					if (wP_state == 'stop') {
						this.wInnerAudioContext.stop();
						// this.wInnerAudioContext.src = null;
					}
					// 开始播放音乐
					if (wP_state == 'play') {
						// 存在音频路径的时候重置路径
						this.wInnerAudioContext.src = wP_src || '';
						this.wInnerAudioContext.play();
					}
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	// 音频组件1
	.wC_audio1 {

		// 组件容器
		.wC_audio1-box {
			display: block;
			width: 140rpx;
			height: 140rpx;
			border-radius: 10rpx;
			border: 1rpx solid #aaaaaa;
			box-sizing: border-box;
			background-color: #eeeeee;
			overflow: hidden;
			position: relative;

			// 封面图
			.wC_audio1-poster {
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				z-index: 10;

				.wC_audio1-posterImg {
					display: block;
					width: 100%;
					height: 100%;
				}
			}

			// 播放图标
			.wC_audio1-play {
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				z-index: 20;
				display: flex;
				justify-content: center;
				align-items: center;

				.wC_audio1-btn {
					flex: none;
					width: 50rpx;
					height: 50rpx;

					// 默认图标
					.wCA1BI-play {
						display: flex;
						width: 100%;
						height: 100%;
						background-color: rgba(0, 0, 0, 0.2);
						border: 4rpx solid #FFFFFF;
						box-sizing: border-box;
						border-radius: 50%;
						justify-content: center;
						align-items: center;
						padding-left: 18rpx;
						box-sizing: border-box;

						.wCA1BIPlay-icon {
							flex: none;
							width: 0;
							height: 0;
							border: 10rpx solid transparent;
							border-left-color: #FFFFFF;
							border-left-width: 20rpx;

						}
					}

					// 播放状态
					.wCA1BI-pause {
						display: flex;
						width: 100%;
						height: 100%;
						background-color: rgba(0, 0, 0, 0.2);
						border: 4rpx solid #FFFFFF;
						box-sizing: border-box;
						border-radius: 50%;
						justify-content: center;
						align-items: center;

						.wCA1BIPause-icon {
							flex: none;
							width: 0;
							height: 0;
							border: 8rpx solid transparent;
							border-color: #FFFFFF;
						}
					}

				}
			}


		}
	}
</style>
